<template>
	<view class="news-detail">
		<div class="news-title">{{newsInfo.title}}</div>
		<div class="news-info">
			<view class="news-author">编辑：{{newsInfo.author||''}}</view>
			<view class="news-date">发布日期：{{newsInfo.posttime||''}}</view>
		</div>
		<div class="news-content">
			<rich-text :nodes="newsInfo.content"></rich-text>
		</div>
	</view>
</template>

<script>
	import {parseTime} from '@/utils/tool.js'
	export default {
		data() {
			return {
				newsInfo:{},
			}
		},
		methods: {
			getNewsDetail(cid,id){
				uni.request({
					url:`https://ku.qingnian8.com/dataApi/news/detail.php?cid=${cid}&id=${id}`,
					success: (res) => {
						console.log('详情res：',res)
						res.data.content=res.data.content.replace(/<img/gi,'<img style="max-width:100%"')		
						
						this.newsInfo = res.data||{}
						
						this.newsInfo.posttime = parseTime(this.newsInfo.posttime)
						uni.setNavigationBarTitle({
							title:this.newsInfo.title||'news详情'
						})
						
						this.setHistory()
						
					}
				})
			},
			
			/**
			 * 设置缓存
			 */
			setHistory(){
				let history = uni.getStorageSync('userHistory')||[]
				console.log('缓存：',history)
				
				let index = history.findIndex(v=>v.id == this.newsInfo.id)
				console.log('index：',index)
				
				if(index>-1){
					let currentArr = history.splice(index,1)
					
					let obj = currentArr[0]
					obj.lookTime = parseTime(Date.now())
					history.unshift(obj)
				}else{
					let obj = this.newsInfo
					obj.lookTime = parseTime(Date.now())
					history.unshift(this.newsInfo)
				}
				uni.setStorageSync('userHistory',history)
				
				// if(history){
				// 	if(history.some(v=>v.id == this.newsInfo.id)){
						
				// 	}else{
				// 		uni.setStorageSync('userHistory',[this.newsInfo,...history])
				// 	}
				// }else{
				// 	uni.setStorageSync('userHistory',[this.newsInfo])
				// }
				
				
			},
		},
		onLoad(e) {
			console.log('路由参数:',e)
			this.getNewsDetail(e.cid,e.id)
		}
	}
</script>

<style lang="scss">
.news-detail{
	padding: 24rpx;
	
	.news-title{
		font-size: 48rpx;
		font-weight: 600;
	}
	
	.news-info{
		background: #f8f8f8;
		padding: 24rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 32rpx;
		margin-top: 32rpx;
		font-size: 28rpx;
	}
	
	.news-content{
		img{
			width: 100%;
		}
	}
}
</style>
